<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

	<ui:composition 
	  xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      template="/view/templates/templateLayout.xhtml">
      
      <ui:define name="conteudo">      	
  	  <link rel="stylesheet" type="text/css" href="./estilos.css"/>
  	  
<h:form id="formEditarVenda" prependId="false">  
	<p:panel id="panelEditarVenda" header="Editar Venda" style="margin-bottom:20px; ">  
	<f:facet name="header">
		<h:outputText value="Editar Venda" styleClass="outputTextTitulo"/>
	</f:facet>
 	<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />  
    	<h:panelGrid id="pgVenda" columns="6" style="margin-top:20px; margin-right:10%;">  
            <h:outputLabel value="Código" styleClass="outputLabel"/>  
            <h:outputLabel value="Nº Nota*" styleClass="outputLabel"/>
            <h:outputLabel value="Nº Série*" styleClass="outputLabel"/>   
            <h:outputLabel value="Pedido" styleClass="outputLabel"/>   
            <h:outputLabel value="Carga" styleClass="outputLabel"/>    
            <h:outputLabel value="Chave de acesso" styleClass="outputLabel"/>       
            <p:inputText value="#{vendaBean.venda.idVenda}" disabled="true" styleClass="inputId"/>     
            <p:inputText value="#{vendaBean.venda.numeroNota}" styleClass="inputData" maxlength="20" required="true" requiredMessage="Informar o Número da Nota!"/>  
  			<p:inputText value="#{vendaBean.venda.numeroSerie}"  styleClass="inputData" maxlength="20" required="true" requiredMessage="Informar o Número de Série!"/>
  			<p:inputText value="#{vendaBean.venda.pedido}"  styleClass="inputData" maxlength="20"/>
  			<p:inputText value="#{vendaBean.venda.carga}"  styleClass="inputData" maxlength="20"/>
  			<p:inputMask value="#{vendaBean.venda.chaveAcesso}" mask="9999.9999.9999.9999.9999.9999.9999.9999.9999.9999.9999" 
  						 styleClass="inputDescricao" maxlength="44" style="width:350px"/>
        </h:panelGrid> 
        
        <h:panelGrid id="pgDate" columns="2" style="margin-right:10%;">
            <h:outputLabel value="Data de Emissão*" styleClass="outputLabel"/>
            <h:outputLabel value="Data de Entrega*" styleClass="outputLabel"/> 
  			<p:inputText value="#{vendaBean.venda.dtEmissao}"  styleClass="inputData" required="true" requiredMessage="Informar a data de emissão!">
  				<f:convertDateTime dateStyle="medium"/>
  				<p:ajax  listener="#{vendaBean.validaData}" render="pgDate" />
  			</p:inputText>
  			<p:inputMask mask="99/99/9999" value="#{vendaBean.venda.dtEntrega}" styleClass="inputData" required="true" requiredMessage="Informar a data de entrega!">
  				<f:convertDateTime dateStyle="medium"/>
  				<p:ajax  listener="#{vendaBean.validaData}" render="pgDate" />
  			</p:inputMask>  
        </h:panelGrid> 
        
        <h:panelGrid id="pgAluno" columns="4" style="margin-right:10%;">  
            <h:outputLabel value="Código" styleClass="outputLabel"/>  
            <h:outputLabel value="Cliente*" styleClass="outputLabel"/>     
            <h:outputLabel  />    
            <h:outputLabel value="CPF" styleClass="outputLabel"/>       
            <p:inputText value="#{vendaBean.venda.aluno.idAluno}" styleClass="inputId" >
            	<p:ajax  listener="#{vendaBean.onChangeAluno}" render="pgAluno, messages" />
            </p:inputText>  
  			<p:autoComplete  value="#{vendaBean.venda.aluno.nome}" 
                    completeMethod="#{vendaBean.completeAluno}"  >
            	<p:ajax event="itemSelect" listener="#{vendaBean.handleSelectAluno}" update="pgAluno" />
			</p:autoComplete>   
  			<p:commandButton icon="ui-icon-search" actionListener="#{vendaBean.openDialogConsultarAluno}" immediate="true"/>
  			<p:inputText value="#{vendaBean.venda.aluno.cpf}" styleClass="inputData" disabled="true"/>
        </h:panelGrid>   
 
        
        <h:panelGrid id="pgProduto" columns="4" style="margin-right:10%;">  
            <h:outputLabel value="Código" styleClass="outputLabel"/>  
            <h:outputLabel value="Produto*" styleClass="outputLabel"/>  
            <h:outputLabel  value="Marca"/>
            <h:outputLabel  />

            <p:inputMask  value="#{vendaBean.itemVenda.produto.idProduto}" styleClass="inputId" >
            	<p:ajax  listener="#{vendaBean.onChangeProduto}" render="pgProduto" />
            </p:inputMask> 
  			<p:autoComplete  value="#{vendaBean.itemVenda.produto.descricao}" 
                    completeMethod="#{vendaBean.completeProduto}" maxlength="40" >
            	<p:ajax event="itemSelect" listener="#{vendaBean.handleSelectProduto}" update="pgProduto" />
			</p:autoComplete>
			<p:inputText value="#{vendaBean.itemVenda.produto.marca.descricao}" styleClass="inputDescricao"/>
  			<p:commandButton  icon="ui-icon-search" actionListener="#{vendaBean.openDialogConsultarProduto}" immediate="true" />  			  
        </h:panelGrid> 
        
       	<h:panelGrid id="pgItemVenda" columns="7" style="margin-right:10%;">  
            <h:outputLabel value="Quantidade" styleClass="outputLabel"/>  
            <h:outputLabel value="Preço (R$)*" styleClass="outputLabel"/> 
            <h:outputLabel  value="Alíquota ICMS (%)" styleClass="outputLabel"/> 
            <h:outputLabel  value="BC ICMS (R$)" styleClass="outputLabel"/>
            <h:outputLabel  value="Valor ICMS (R$)" styleClass="outputLabel"/>            
            <h:outputLabel  value="Valor IPI (R$)" styleClass="outputLabel"/>
            <h:outputLabel  />
              			  
  			<p:inputText value="#{vendaBean.itemVenda.quantidade}" styleClass="inputId" style="width:65px;" >
  				<f:convertNumber type="number"/>
  				<p:ajax  listener="#{vendaBean.calculaBCIcms}" render="pgItemVenda" />
  			</p:inputText>
  			<p:inputText id="inputPrecoVenda" value="#{vendaBean.itemVenda.precoVenda}" styleClass="inputId" style="width:60px;" disabled="true">
  				<f:convertNumber type="number" />
 				<p:ajax  listener="#{vendaBean.calculaBCIcms}" render="pgItemVenda" />
  			</p:inputText>
  			<p:inputText value="#{vendaBean.itemVenda.aliquotaIcms}" styleClass="inputId" style="width:105px;">
  				<f:convertNumber type="number" />
  				<p:ajax  listener="#{vendaBean.calculaValorIcms}" render="pgItemVenda" />
  			</p:inputText>
  			<p:inputText id="inputBcIcms" value="#{vendaBean.itemVenda.bcIcms}" styleClass="inputId" style="width:80px;">
  				<f:convertNumber type="number" />
  			</p:inputText>
  			<p:inputText id="inputValorIcms" value="#{vendaBean.itemVenda.valorIcms}" styleClass="inputId" style="width:90px;">
  				<f:convertNumber type="number" />
  			</p:inputText>

  			<p:inputText value="#{vendaBean.itemVenda.valorIpi}" styleClass="inputId" style="width:70px;">
  				<f:convertNumber type="number" />
  			</p:inputText>
  			<p:commandButton icon="ui-icon-circle-plus" action="#{vendaBean.addItemVenda}" update="messages"/>
        </h:panelGrid> 
        
        
        <p:dataTable  id="tableItemVenda" value="#{vendaBean.arrayItemVenda}" var="itemVenda" style="margin-top:30px; width:80%"  
        				rowKey="#{itemVenda.idItemVenda}" selection="#{vendaBean.itemVendaSelect}" >
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Código" />
				</f:facet>
				<h:outputText value="#{itemVenda.produto.idProduto}" />
			</p:column>
			<p:column style="width:20%">
				<f:facet name="header">
					<h:outputText value="Produto" />
				</f:facet>
				<h:outputText value="#{itemVenda.produto.descricao}" />
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Quantidade" />
				</f:facet>
				<h:outputText value="#{itemVenda.quantidade}" />
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Preço Unitário" />
				</f:facet>
				<h:outputText value="#{itemVenda.precoCusto}" >
					<f:convertNumber type="currency" currencySymbol="R$ "/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Preço Total" />
				</f:facet>
				<h:outputText value="#{itemVenda.precoTotal}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="BC ICMS" />
				</f:facet>
				<h:outputText value="#{itemVenda.bcIcms}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Valor ICMS" />
				</f:facet>
				<h:outputText value="#{itemVenda.valorIcms}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Valor IPI" />
				</f:facet>
				<h:outputText value="#{itemVenda.valorIpi}" >
					<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2"/>
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Alíquota ICMS (%)" />
				</f:facet>
				<h:outputText value="#{itemVenda.aliquotaIcms}" >
					<f:convertNumber  />
				</h:outputText>
			</p:column>
			<p:column style="width:5%; text-align:center">
				<f:facet name="header">
					<h:outputText value="Alíquota IPI (%)" />
				</f:facet>
				<h:outputText value="#{itemVenda.aliquotaIpi}" >
					<f:convertNumber type="percentage" />
				</h:outputText>
			</p:column>
       	 	<p:column style="width:4%">  
        		<f:facet name="header">
        	 		<h:outputText value="Remover" />
        		</f:facet>
         		<p:commandButton icon="ui-icon-trash" actionListener="#{vendaBean.removeItem}" process="@this" update="tableItemVenda" >
         			<f:setPropertyActionListener target="#{vendaBean.itemVendaSelect}" value="#{itemVenda}" />
         		</p:commandButton>         			
       	 	</p:column >  
       	 	<f:facet name="footer" >
       	 		<h:outputText value="Total: R$ #{vendaBean.somaValorTotal}" >
       	 			<f:convertNumber maxFractionDigits="2" minFractionDigits="2"/>
       	 		</h:outputText>
       	 	</f:facet>        
        </p:dataTable>
        
        <h:panelGrid id="pgTributos" columns="5" style="margin-right:10%;">  
           	<h:outputLabel value="BC ICMS" styleClass="outputLabel"/>
           	<h:outputLabel value="Valor ICMS " styleClass="outputLabel"/>
           	<h:outputLabel value="Valor IPI" styleClass="outputLabel"/> 
           	<h:outputLabel value="BC ICMS Subst." styleClass="outputLabel"/>
           	<h:outputLabel value="Valor ICMS Subst." styleClass="outputLabel"/>              	
           	
           	<p:inputText value="#{vendaBean.venda.bcIcms}" styleClass="inputId" style="width:70px" disabled="true" /> 
 			<p:inputText value="#{vendaBean.venda.valorIcms}" styleClass="inputId" style="width:70px" disabled="true"/>
 			<p:inputText value="#{vendaBean.venda.valorIpi}" styleClass="inputId" style="width:70px" disabled="true"/>
 			<p:inputText value="#{vendaBean.venda.bcIcmsSubst}" styleClass="inputId" style="width:90px" />
 			<p:inputText value="#{vendaBean.venda.valorIcmsSubst}" styleClass="inputId" style="width:105px" />
 			
        </h:panelGrid> 
        
        <h:panelGrid id="pgTotal" columns="6" style="margin-right:10%;">  
           	<h:outputLabel value="Frete (R$)" styleClass="outputLabel"/>
           	<h:outputLabel value="Seguro (R$) " styleClass="outputLabel"/>
           	<h:outputLabel value="Outras Despesas Acessórias (R$)" styleClass="outputLabel"/>
           	<h:outputLabel value="Desconto (%)" styleClass="outputLabel"/>
           	<h:outputLabel  />
           	<h:outputLabel value="Total da Venda(R$)" styleClass="outputLabel"/> 
           	
           	<p:inputText value="#{vendaBean.venda.frete}" styleClass="inputId" style="width:70px" >
           		<p:ajax  listener="#{vendaBean.atualizaTotal}" render="pgTotal" />
           	</p:inputText>
           	<p:inputText value="#{vendaBean.venda.seguro}" styleClass="inputId" style="width:70px">
           		<p:ajax  listener="#{vendaBean.atualizaTotal}" render="pgTotal" />
           	</p:inputText>  
           	<p:inputText value="#{vendaBean.venda.despesasAcessorias}" styleClass="inputId" style="width:190px">
           		<p:ajax  listener="#{vendaBean.atualizaTotal}" render="pgTotal" />
           	</p:inputText> 
           	<p:inputText value="#{vendaBean.venda.desconto}" styleClass="inputId" style="width:70px">
           		<p:ajax  listener="#{vendaBean.atualizaDesconto}" render="pgCondicaoPagamento" />
           	</p:inputText> 
           	<p:commandButton icon="ui-icon-calculator" action="#{vendaBean.calculaTotalVenda}" process="@this pgTotal" disabled="#{vendaBean.disableCalculaTotalVenda()}" style="text-align:left; float:left"/>
  			<p:inputText id="idValorTotal" value="#{vendaBean.venda.valorTotal}"  styleClass="inputData" disabled="true"  >
  				<f:convertNumber type="currency" currencySymbol="R$ " maxFractionDigits="2"/>
  			</p:inputText>  
        </h:panelGrid> 

        	<h:panelGrid id="pgCondicaoPagamento" columns="5" style="margin-right:10%;">  
           	 	<h:outputLabel value="Código" styleClass="outputLabel"/>  
            	<h:outputLabel value="Condição de Pagamento*" styleClass="outputLabel"/>      
            	<h:outputLabel  />       
            	<h:outputLabel value="Código" styleClass="outputLabel"/>  
            	<h:outputLabel value="Forma de Pagamento*" styleClass="outputLabel"/>    
            	<p:inputText value="#{vendaBean.venda.condicaoPagamento.idCondicaoPagamento}" styleClass="inputId" disabled="true"/>  
  				<p:inputText value="#{vendaBean.venda.condicaoPagamento.descricao}"  styleClass="inputDescricao" style="width:150px;" immediate="true" disabled="true"/>  
  				<p:commandButton icon="ui-icon-search" actionListener="#{vendaBean.openDialogConsultarCondicaoPagamento}" immediate="true" disabled="#{vendaBean.disableCondicao()}"/>  
            	<p:inputText value="#{vendaBean.venda.condicaoPagamento.formaPagamento.idFormaPagamento}" styleClass="inputId" disabled="true"/>  
  				<p:inputText value="#{vendaBean.venda.condicaoPagamento.formaPagamento.descricao}"  styleClass="inputDescricao" style="width:150px;" disabled="true"/>  
        	</h:panelGrid> 


        	<p:dataTable  id="tableParcela" value="#{vendaBean.listContaReceber}" var="cr" style="margin-top:30px; width:20%">        				
				<p:column>
					<f:facet name="header">
						<h:outputText value="Data de Vencimento" />
					</f:facet>
					<h:outputText value="#{cr.dtVencimento}" >
						<f:convertDateTime dateStyle="medium"/>
					</h:outputText>
				</p:column>
				<p:column>
					<f:facet name="header">
						<h:outputText value="Valor" />
					</f:facet>
					<h:outputText value="#{cr.valor}" >
						<f:convertNumber pattern="R$ ###,###.##"/>
					</h:outputText>
				</p:column>      
    	    </p:dataTable>
    </p:panel>  
    <p:commandButton value="Salvar" actionListener="#{vendaBean.saveVenda}" update="panelEditarVenda" style="margin-right:20px; margin-left:10px;" /> 
	<p:commandButton value="Voltar" action="#{vendaBean.consultarVenda}" immediate="true" style="margin-right:20px;" /> 
</h:form>  	
<ui:include src="/view/venda/dialogConsultarAluno.xhtml"></ui:include>
<ui:include src="/view/venda/dialogConsultarProduto.xhtml"></ui:include>
<ui:include src="/view/venda/dialogConsultarCondicaoPagamento.xhtml"></ui:include>


</ui:define>
</ui:composition>
